package com.jspx.txweb.table;

import com.jspx.boot.sign.SexType;
import com.jspx.json.JSONObject;
import com.jspx.sober.annotation.Column;
import com.jspx.sober.annotation.IDType;
import com.jspx.sober.annotation.Id;
import com.jspx.sober.annotation.Table;
import com.jspx.sober.table.OperateTable;
import com.jspx.utils.StringUtil;

/**
 * 用于微信，QQ等系统登陆
 */

@Table(name = "jspx_oauth_openid", caption = "第三方登陆数据", cache = true)
public class OAuthOpenId extends OperateTable {
    @Id(auto = true, type = IDType.serial)
    @Column(caption = "ID", notNull = true)
    private long id;

    //本站的用户UID
    @Column(caption = "UID", notNull = true)
    private long uid;

    //作为唯一标识
    @Column(caption = "openId", length = 64, dataType = "isLengthBetween(1,64)", notNull = true)
    private String openId = StringUtil.empty;

    //微信 认证企业才有这
    @Column(caption = "unionid", length = 120, dataType = "isLengthBetween(1,120)")
    private String unionid = StringUtil.empty;

    //qq在做一些 外挂发布的时候会用到
    @Column(caption = "accessToken", length = 240, dataType = "isLengthBetween(1,240)")
    private String accessToken;

    //第三方软件的昵称
    @Column(caption = "昵称", length = 50, dataType = "isLengthBetween(2,50)", notNull = true)
    private String name = StringUtil.empty;

    @Column(caption = "性别", length = 8, dataType = "isLengthBetween(1,8)", notNull = true)
    private String sex = StringUtil.empty;

    @Column(caption = "城市", length = 50, dataType = "isLengthBetween(2,60)")
    private String city = StringUtil.empty;

    @Column(caption = "国家", length = 50, dataType = "isLengthBetween(2,60)")
    private String country = StringUtil.empty;

    @Column(caption = "省份", length = 50, dataType = "isLengthBetween(2,60)")
    private String province = StringUtil.empty;

    //头像
    @Column(caption = "头像URL", length = 240)
    private String faceImage = StringUtil.empty;

    //qq ,微信，sina
    @Column(caption = "命名空间", length = 50, notNull = true)
    private String namespace = StringUtil.empty;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getOpenId() {
        return openId;
    }

    public void setOpenId(String openId) {
        this.openId = openId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public long getUid() {
        return uid;
    }

    public void setUid(long uid) {
        this.uid = uid;
    }

    public String getFaceImage() {
        return faceImage;
    }

    public void setFaceImage(String faceImage) {
        this.faceImage = faceImage;
    }

    public String getUnionid() {
        return unionid;
    }

    public void setUnionid(String unionid) {
        this.unionid = unionid;
    }

    public String getNamespace() {
        return namespace;
    }

    public void setNamespace(String namespace) {
        this.namespace = namespace;
    }

    public String getAccessToken() {
        if (accessToken==null) return StringUtil.empty;
        return accessToken;
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public Member createMember()
    {
        Member member = new Member();
        member.setName(name);
        member.setSex(SexType.getSexZh(SexType.getSex(sex)));
        member.setFaceImage(faceImage);
        member.setCity(city);
        member.setCountry(country);
        member.setProvince(province);
        return member;
    }

    public OAuthOpenId()
    {

    }
    public OAuthOpenId(JSONObject json)
    {
        if (json==null) return;
        //为了方便移动应用调用
        openId = json.getString("openId");
        unionid = json.getString("unionid");
        accessToken = json.getString("accessToken");
        name = json.getString("name");
        sex = json.getString("sex");
        country = json.getString("country");
        province = json.getString("province");
        faceImage = json.getString("faceImage");
        namespace = json.getString("namespace");
    }
    public JSONObject toJson()
    {
        //为了方便移动应用调用
        JSONObject json =new JSONObject();
        json.put("openId",openId);
        json.put("unionid",unionid);
        json.put("accessToken",accessToken);
        json.put("name",name);
        json.put("sex",sex);
        json.put("country",country);
        json.put("province",province);
        json.put("faceImage",faceImage);
        json.put("namespace",namespace);
       return json;
    }
}
